-- 可供调用的接口函数：
-- MQTTSend			LUA函数形式：MQTTSend(byte[] buffer)(bool res,string error)
-- TCPInit			LUA函数形式：TCPInit(string host,int port)(bool res,string error)
-- TCPSend			LUA函数形式：TCPSend(string key,byte[] buffer)(bool res,string error)
-- TCPClose			LUA函数形式：TCPClose()
-- RTDBWrite		TODO:未实现
-- RTDBRead			TODO:未实现
-- HisDBWrite		TODO:未实现
-- HisDBRead		TODO:未实现
-- TimerInit		LUA函数形式：TimerInit(Int64 ms)(bool res,string error)
-- TimerStart		LUA函数形式：TimerStart()
-- TimerStop		LUA函数形式：TimerStop()
-- TimerClose		LUA函数形式：TimerClose()

DirectSend = {
    host = "127.0.0.1",
    port = 502,
    tcpKey = nil
}
-- new(string param) (bool res,string err)
function DirectSend:new(param)    
    -- print(package.path)  
    -- local m = require("module")
    -- print("module") 
    -- print(m.constant)
 
    -- m.func3()
    -- print("func3") 

    local stat=assert(loadstring(param))()
    local o = {}
    setmetatable(o, self)
    self.__index = self
    self.host = stat.host or "127.0.0.1"
    self.port = stat.port or 502    
    self.tcpKey = nil
    return true,nil,o
end
-- Start() (bool res,string err)
function DirectSend:Start()
    print("--------"..self.host)
    print("--------"..self.port)
    return self.TCPInit(self.host,self.port)
end
-- Stop() (bool res,string err)
function DirectSend:Stop()
end
-- MQTTReciveEvent(byte[] buffer) (bool res,string err)
function DirectSend:MQTTReciveEvent(buffer)
    -- MQTTSend(buffer)
    self.TCPSend(self.tcpKey, buffer)
end
-- TCPReciveEvent(string key, byte[] buffer)
function DirectSend:TCPReciveEvent(key, buffer)
    local frame = "TCP Receive:"
    for i=1, #(buffer) do      
        frame = frame..string.format(" 0x%02X",buffer[i])
    end  
    print(frame)
    -- TCPSend(key, buffer)
    self.MQTTSend(buffer)
end
-- TCPAcceptEvent(string key)
function DirectSend:TCPAcceptEvent(key)
    self.tcpKey = key
    print("TCP accept:"..key)
end
-- TimerElapsed()
function DirectSend:TimerElapsed()
end
